package com.ht.modules.system.config;


import org.apache.commons.codec.binary.Base64;

import jakarta.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigInteger;

public class Test {
    public static void main(String[] args) {

        String string = "OUIxNENEAFQ0Y3M0NDNTQ5RDk0NEQwRkMxMjY1NkJCNTVPOTM5MDE5RjNERjk5OURDRTlCRTJGMjZDQjYyNDM3RkM5MDkzNTg3NTg0QjcxRjQxQUQ3RTZBNjYxRTVCQTI2OTBEQUI4MUIzRjZBMTMzRUE0MzAxNDU1MTJBM0VBN0M1QTAxNDE0RkY1NEVCNUVFQTIzOTU1REM1QTlFNEY3MTVFMjRCMEQ3NDJDQzE1N0ZDM0Q5RjAyQjFGRDJCRTUzN0YxNjg3MUVEMDE0QkUxNEI2QTY5MDA1NEIzMjlERUZGNDEyNTk4RkFDQzBFNTNCQUVDQ0NEQjcyNjU4RDVFRDFGMkUwMDA4NzEwNjlDQ0VCRDNEMDNGRDNDNEFBNDk3NzRGMkJCMEZDM0Y4NzU2RDMzRjA4MzNGQTU0RTdBQzQ5MEMyMTM2ODg5RTdEQkEyRDc1N0ZBQjdCNTM5REM4NjFDNjJGMEZGRDhGNjU2MjgyOEUyQTI5M0YzNjM5RkU5RjM3OEUyQ0FDODQ3RDAyQzlDRDZBRTlGQjgyMzk5NEEyOEQ5OUJEMkQyOTc5MEJDQjBCOEM5RjBGRjM5QTgyODA5MkNBRkQ0ODJFODZBRURGN0JDMzlGQkY5RjYyNkY1MUFDRjIxMTQ2RDczMEU1QTgzNUZGQzc2REQwMjYzNDAzOEI0Mjk1MDA3RkYxMTNCRTc5ODgwMTkwMTZENDRBNTA0MTM1OTMyNzUxMkE4QjEwREM3ODQ4NjYwNEE0RUFGRkRDNzg0ODY2MDRBNEVBRkZEQzc4NDg2NjA0QTRFQUZGREM3ODQ4NjYwNEE0RUFGRkRDNzg0ODY2MDRBNEVBRkZEQzc4NDg2NjA0QTRFQUZGNjRDN0Q3QThFNjkwNDA2QU9DRjBCMDdDRTg2NTk2MUY0NDBBNjE1NTQxQzUwREY0RA";
        String filePath = "d:\\test1.txt";
        stirng2File(string, filePath);

    }

    public static void stirng2File(String base64Code, String targetPath)  {
//        System.out.println("20个字符" + base64Code.substring(0, 50));
//        System.out.println("BASE64转译String333:" + base64Code.length());
        byte[] buffer;
        FileOutputStream out = null;
        FileOutputStream out2 = null;
        try {
            Base64 base64 = new Base64();
            //解码
            buffer = base64.decode(base64Code);

        String sendString;
		try {
			sendString = new String(buffer ,"UTF-8");
			System.out.println(new String(buffer ,"GB2312"));
			System.out.println(sendString);

			System.out.println(binaryToHexString(buffer));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		transform(buffer);
            //buffer = new BASE64Decoder().decodeBuffer(base64Code);
            System.out.println("BASE64转译byte111:" + buffer.length);
            //System.out.println("数据为"+temp);
            //System.out.println("buffer长度"+buffer.length);
//            out = new FileOutputStream(targetPath);
//            out2 = new FileOutputStream("D:\\test\\1.txt");
//            out.write(buffer);

            byte2File(buffer, "D:\\", "configVo.txt");
//            out2.write(buffer);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (out2 != null) {
                try {
                    out2.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }

    /**
     * 根据byte数组，生成文件
     * @param bfile 文件数组
     * @param filePath 文件存放路径
     * @param fileName 文件名称
     */
    public static void byte2File(byte[] bytes,String filePath,String fileName){
        BufferedOutputStream bos=null;
        FileOutputStream fos=null;
        File file=null;
        try{
            File dir=new File(filePath);
            if(!dir.exists() && !dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file=new File(filePath+fileName);
       new   ByteArrayOutputStream();
            fos=new FileOutputStream(file);
            bos=new BufferedOutputStream(fos);
            bos.write(bytes);

            InputStream sbs = new ByteArrayInputStream(bytes);
            System.out.println(sbs.toString());
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        finally{
            try{
                if(bos != null){
                    bos.close();
                }
                if(fos != null){
                    fos.close();
                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
    /**
     * 获得指定文件的byte数组
     * @param filePath 文件绝对路径
     * @return
     */
    public static byte[] file2Byte(String filePath){
        ByteArrayOutputStream bos=null;
        BufferedInputStream in=null;
        try{
            File file=new File(filePath);
            if(!file.exists()){
                throw new FileNotFoundException("file not exists");
            }
            bos=new ByteArrayOutputStream((int)file.length());
            in=new BufferedInputStream(new FileInputStream(file));
            int buf_size=1024;
            byte[] buffer=new byte[buf_size];
            int len=0;
            while(-1 != (len=in.read(buffer,0,buf_size))){
                bos.write(buffer,0,len);
            }
            return bos.toByteArray();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
        finally{
            try{
                if(in!=null){
                    in.close();
                }
                if(bos!=null){
                    bos.close();
                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }


    //字符串转换成数组
    public static byte[] toByteArray(String hexString) {
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {// 因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }
    /**
     * 字符串转换成字节流并输出
     *
     */
    public void file(HttpServletResponse response,String xx){

//        byte[] bytes = CommonUtil.toByteArray(xx);
        byte[] bytes =  xx.getBytes();

        OutputStream os = null;
        try {
            os = response.getOutputStream();
            response.addHeader("Content-Type", "image/jpeg");
            os.write(bytes);
            os.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String binaryToHexString(byte[] bytes) {
        if (bytes == null || bytes.length == 0) return "";
        String hexStr = "0123456789ABCDEF";
        StringBuilder result = new StringBuilder("");
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result.append(hex);
            //result += hex + " ";//可加空格分割
        }
        return result.toString();
    }


public static void transform(byte[] bytes) {
        System.out.println("2进制："   + binary(bytes, 2));
        System.out.println("5进制："   + binary(bytes, 5));
        System.out.println("8进制："   + binary(bytes, 8));
        System.out.println("16进制："  + binary(bytes, 16));
        System.out.println("32进制："  + binary(bytes, 32));
        System.out.println("64进制："  + binary(bytes, 64));// 这个已经超出范围，超出范围后变为10进制显示
}
public static String binary(byte[] bytes, int radix){
    return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
}
}
